package com.ruoyi.common.core.dsl

import groovy.time.Duration
import groovy.time.TimeCategory

/**
 * 闭包模块定义
 */
class DslSpec {
    Closure givenCall
    long rate
    String cron
    Closure scheduleCall
    Closure executeCall

    /**
     * 定时任务执行频次设置模块
     */
    def every = { Duration time ->
        this.rate = time.toMilliseconds()
    }

    /**
     * 定时任务执行频次设置模块(cron表达式)
     */
    def when = { String expression ->
        this.cron = expression
    }

    /**
     * 定时任务执行模块代码
     */
    def run = { Closure c ->
        this.scheduleCall = c
    }

    /**
     * 常量定义模块
     * @param closure
     * @return
     */
    def given(Closure closure) {
        closure.resolveStrategy = Closure.DELEGATE_ONLY
        givenCall = closure
    }

    /**
     * 定时执行模块
     * @param closure
     * @return
     */
    def schedule(Closure closure) {
        closure.resolveStrategy = Closure.DELEGATE_ONLY
        use TimeCategory, {
            closure()
        }
    }


    /**
     * 常规代码执行模块
     * @param closure
     * @return
     */
    def execute(Closure closure) {
        closure.resolveStrategy = Closure.DELEGATE_FIRST
        executeCall = closure
    }
}
